import * as request from '../constant/fetch-request'
import HTTP_CODE from '../constant/http-code'
import {message} from 'antd'
import { convertContent } from '../constant/lang-util'
import {getMyStudents} from './myStudents'

export const addStudentTag = (color, name, programId) => {
  const data = {
    color,
    name,
    programId
  }
  return dispatch => {
    (async () => {
      const res = await request.post(`../api/programs/tags`, data)
      if (res.status === HTTP_CODE.CREATED) {
        message.success(convertContent('添加成功'))
        dispatch(getStudentTagsByProgramId(programId))
      }
    })()
  }
}

export const getStudentTagsByProgramId = (programId) => {
  return dispatch => {
    (async () => {
      const res = await request.get(`../api/programs/${programId}/tags`)
      if (res.status === HTTP_CODE.OK) {
        dispatch({
          type: 'GET_USER_TAGS',
          data: res.body
        })
      }
    })()
  }
}

export const editStudentTag = (tagId, tagColor, tagName, programId) => {
  const data = {
    color: tagColor,
    name: tagName
  }
  return dispatch => {
    (async () => {
      const res = await request.update(`../api/programs/tags/${tagId}`, data)
      if (res.status === HTTP_CODE.NO_CONTENT) {
        message.success(convertContent('修改成功'))
        dispatch(getStudentTagsByProgramId(programId))
      }
    })()
  }
}

export const deleteStudentTag = (tagId, programId) => {
  return dispatch => {
    (async () => {
      const res = await request.del(`../api/programs/tags/${tagId}`)
      if (res.status === HTTP_CODE.NO_CONTENT) {
        message.success(convertContent('删除成功'))
        dispatch(getStudentTagsByProgramId(programId))
      }
    })()
  }
}
export const editStudentTags = (programId, studentId, tagIds) => {
  tagIds = tagIds.length === 0 ? ',' : tagIds

  return dispatch => {
    (async () => {
      await request.update(`../api/programs/${programId}/students/${studentId}/tags/${tagIds || ','}`)
      dispatch(getMyStudents(programId))
    })()
  }
}
